/*
 * 컴파일을 위해 올려 놓은 소스 입니다.
 * 서버에 반영하지 마시고 다른국가에 적용할 경우 서버에 있는 class 를 카피해서 사용하십시오.
 * 
 */
/*
 * Created on 2004. 9. 24.
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package com.pushmsg.OmaOtaConf; 

/** 
 * @author dgchun
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class OmaOta 
{
	private final int MAXMSGSIZE = 140;

	final static int BROWSER_WAP_CSD = 0x00;
	final static int BROWSER_WAP_GPRS = 0x01;	
	final static int MMS_WAP_GPRS = 0x04;
	final static int EMAIL_HTTP_GPRS = 0x06;
	final static int BROWSER_HTTP_GPRS = 0x07;
	final static int BROWSER_HTTP_CSD = 0x08;
	final static int MMS_HTTP_GPRS = 0x09;
	final static int DS_HTTP_GPRS = 0x0A;
	
	OmaOtaWdpHdr wdpHdr = null;
	OmaOtaWspHdr  wspHdr = null;
	OmaOtaBody  body = null;
	private int segno=0;
	private byte[][] segment;
	
	public OmaOta(int otatype)
	{
		wdpHdr = new OmaOtaWdpHdr();
		wspHdr = new OmaOtaWspHdr();
		
		switch(otatype) 
		{
			case BROWSER_WAP_GPRS:
			case BROWSER_WAP_CSD:
			case BROWSER_HTTP_GPRS:
			case BROWSER_HTTP_CSD:
				body = (OmaOtaBody)(new OmaOtaBrowser(otatype));
				break;
			case MMS_HTTP_GPRS:
			case MMS_WAP_GPRS:
				body = (OmaOtaBody)(new OmaOtaMms(otatype));
				break;
			case EMAIL_HTTP_GPRS:
				body = (OmaOtaBody)(new OmaOtaEmail());
				break;
			case DS_HTTP_GPRS:
				body = (OmaOtaDs)(new OmaOtaDs(otatype));
				break;				
		}
	} 
	
	public void setPin(String pin)
	{
		wspHdr.setPin(pin);
	}
	
	/*
	public void setPinType(String type)
	{
		wspHdr.setPinType(type);
	}*/
	public OmaOtaBody getBody()
	{
		return body;
	}
	
	public boolean process()
	{
		boolean flag=true;
		
		switch(body.getOtaType())
		{
			case EMAIL_HTTP_GPRS:
				OmaOtaEmail ooe = (OmaOtaEmail)body;
				ooe.process();
				break;
			case BROWSER_WAP_GPRS:
			case BROWSER_WAP_CSD:
			case BROWSER_HTTP_GPRS:
			case BROWSER_HTTP_CSD:	
				OmaOtaBrowser oob = (OmaOtaBrowser)body;
				oob.process();
				break;
			case MMS_HTTP_GPRS:
				OmaOtaMms oom = (OmaOtaMms)body;
				oom.process();
				break;
			case DS_HTTP_GPRS:
				OmaOtaDs ood= (OmaOtaDs)body;
				ood.process();
				break;
		}
		byte[] temp = new byte[body.length()];
		System.arraycopy(body.getData(), 0, temp, 0, temp.length);
		//wspHdr.CalculateMac(temp);
		computeSegNo();
		segment = new byte[segno][];
		makeSegment();	
		return flag;
	}
	
	public int getNumSegment()
	{
		return segno;
	}
	
	public byte[] getSegment(int i)
	{
		return segment[i];
	}
	public int  getHeadLength( )
	{
			//return wdpHdr.getLength()+wspHdr.getLength();
//		System.out.println( " wdpHdr.getLength()="+wdpHdr.getLength() );
		 
			return wdpHdr.getLength() ;
	}		
	
	private void computeSegNo()
	{
		//System.out.println("*************");
		int bodylen = body.length();
		segno++;
		//System.out.println("BODY LEN=" + bodylen);
		bodylen = bodylen - (140 - wdpHdr.getLength()-wspHdr.getLength());
//		System.out.println("BODY LEN=" + bodylen);
		if(bodylen<0)
			segno--;	
		while(bodylen>(140-wdpHdr.getLength()))
		{
			//System.out.println("BODY LEN=" + bodylen);
			segno++;			
			bodylen = bodylen - (140-wdpHdr.getLength());
		}
		segno++;
		//System.out.println("Segment number=" + segno);	
		//System.out.println("*************");
	}
	
	private void makeSegment()
	{
		int segndx=0, ndx=0, maxmsgsize=MAXMSGSIZE;
		int bodystart=0, bodylen = body.length();
		byte[] bodydata = body.getData();
		byte[] buf = new byte[MAXMSGSIZE];
		wdpHdr.setTotalno(segno);
					
		while(true)
		{
			wdpHdr.setCurno(segndx+1);	
			for(int i=0; i<wdpHdr.getLength(); i++)
			{
				buf[ndx++]=wdpHdr.getData()[i];	
				maxmsgsize--;			
			}
			if(segndx==0)
			{
				for(int i=0; i<wspHdr.getLength(); i++)
				{
					buf[ndx++]=wspHdr.getData()[i];
					maxmsgsize--;
				}
			}
			for(int i=0; i<maxmsgsize&&i<bodylen; i++)
			{
				buf[ndx++]=bodydata[bodystart+i];
			}
			//System.out.println("-----");
			//System.out.println(segndx+" "+ndx);
			//System.out.println("-----");
			segment[segndx]=new byte[ndx];
			for(int i=0; i<ndx; i++)
				segment[segndx][i]=buf[i];
			bodylen -= maxmsgsize;
			bodystart += maxmsgsize;
			ndx=0;
			if(bodylen<=0)
				break;
			maxmsgsize=MAXMSGSIZE;
			segndx++;				
		}
	}	
	/**
	 * @return Returns the wdpHdr.
	 */
	public OmaOtaWdpHdr getWdpHdr() {
		return wdpHdr;
	}
	/**
	 * @param wdpHdr The wdpHdr to set.
	 */
	public void setWdpHdr(OmaOtaWdpHdr wdpHdr) {
		this.wdpHdr = wdpHdr;
	}
	/**
	 * @return Returns the wspHdr.
	 */
	public OmaOtaWspHdr getWspHdr() {
		return wspHdr;
	}
	/**
	 * @param wspHdr The wspHdr to set.
	 */
	public void setWspHdr(OmaOtaWspHdr wspHdr) {
		this.wspHdr = wspHdr;
	}
	/**
	 * @return Returns the mAXMSGSIZE.
	 */
	public int getMAXMSGSIZE() {
		return MAXMSGSIZE;
	}
	/**
	 * @return Returns the segno.
	 */
	public int getSegno() {
		return segno;
	}
	/**
	 * @param segno The segno to set.
	 */
	public void setSegno(int segno) {
		this.segno = segno;
	}	
}
